﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;

namespace tp2
{
    public partial class Main : Form
    {
        ChessGame chessGame;

        public Main()
        {
            InitializeComponent();
        }

        #region Methods

        private void StartResolve()
        {
            DialogResult result = System.Windows.Forms.DialogResult.Yes;

            if (rbToutes.Checked && nudN.Value > 12)
                result = MessageBox.Show("Le traitement peut être long avec une valeur supérieur à 12. Voulez-vous continuer?", "Attention", MessageBoxButtons.YesNo);

            if (result == System.Windows.Forms.DialogResult.Yes)
            {
                btnResourdre.Enabled = false;
                bgwResolution.RunWorkerAsync();
            }
        }

        private void DoResolve()
        {
            int n = (int)nudN.Value;
            bool allSolutions = rbToutes.Checked;
            chessGame = new ChessGame();
            chessGame.Resolve(n, allSolutions, bgwResolution);
            chessGame.CreateSolutionsBitmap();
        }

        private void ShowSolutionsFound()
        {
            int ctr = 0;
            Label lblTuplesProduits;
            GroupBox gbSolution;
            PictureBox pbSolution;
            lblNbrTuplesTotal.Text = String.Format(lblNbrTuplesTotal.Tag.ToString(), chessGame.NbrTuplesTotal);
            lblNbrResultats.Text = String.Format(lblNbrResultats.Tag.ToString(), chessGame.SolutionsCount);
            flowLayoutPanel1.SuspendLayout();
            flowLayoutPanel1.Controls.Clear();
            foreach (Solution solution in chessGame.Solutions.GetRange(0, Math.Min(chessGame.SolutionsCount, 500)))
            {
                gbSolution = new GroupBox();
                pbSolution = new PictureBox();
                lblTuplesProduits = new Label();
                lblTuplesProduits.Text = String.Format("{0} tuples produits", solution.TriesBeforeFound);
                pbSolution.BackgroundImage = solution.Image;
                pbSolution.BackgroundImageLayout = ImageLayout.Zoom;
                gbSolution.MinimumSize = new Size(100, 100);
                gbSolution.Size = new Size((int)nudN.Value * 25, (int)nudN.Value * 25 + lblTuplesProduits.Height + lblTuplesProduits.Margin.Top + lblTuplesProduits.Margin.Bottom);
                gbSolution.Text = String.Format("Solution {0}", ++ctr);
                gbSolution.Controls.Add(pbSolution);
                gbSolution.Controls.Add(lblTuplesProduits);
                lblTuplesProduits.Dock = DockStyle.Top;
                pbSolution.Dock = DockStyle.Fill;
                flowLayoutPanel1.Controls.Add(gbSolution);
            }
            flowLayoutPanel1.ResumeLayout();
        }

        #endregion

        #region Events

        private void btnResourdre_Click(object sender, EventArgs e)
        {
            StartResolve();
        }

        private void bgwResolution_DoWork(object sender, DoWorkEventArgs e)
        {
            DoResolve();
        }

        private void bgwResolution_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            pbResolution.Value = e.ProgressPercentage;
        }

        private void bgwResolution_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            pbResolution.Value = 100;
            btnResourdre.Enabled = true;
            ShowSolutionsFound();
        }

        #endregion

    }
}
